import { _decorator, Component, KeyCode, Node, PhysicsSystem2D, Rect } from 'cc';
import { PlayerState } from './PlayerState';
import { StateDefine } from '../../Config';
import { Enemy } from '../../Enemy/Enemy';
const { ccclass, property } = _decorator;

@ccclass('PlayerCounterAttackState')
export class PlayerCounterAttackState extends PlayerState {
    onEnter() {
        super.onEnter();
        this.stateTime = this.player.counterAttackDuration;
    }
    onExit() {
        super.onExit();
    }
    update(deltaTime: number) {
        this.player.ZeroVelocity();
        super.update(deltaTime)
        const pos = this.player.AttackCheck.getWorldPosition();
        //偏移问题
        const box = new Rect(pos.x+(this.player.facingDir-1)*this.player.AttackCheckR/2,pos.y, this.player.AttackCheckR, this.player.AttackCheckR);
        const colliderList = PhysicsSystem2D.instance.testAABB(box); 
        
        if(this.stateTime>0){
            for (const collider of colliderList) {
                const enemyComponent = collider.getComponent(Enemy);
                if (enemyComponent) {
                    console.info(collider.name);
                    if(enemyComponent.CanBeStunned()){
                        this.stateTime = 10;
                        this.animation.play(StateDefine.CounterSuccessful);
                    }
                }
            }
        }
        if(this.triggerCalled||(this.stateTime<0&&!this.player.pressedKeys[KeyCode.KEY_K])){
            this.player.stateMgr.transit(StateDefine.Idle);
        }
            
    }
    onDestory() { }

    canTransit(to: StateDefine): boolean {
        // if(this.player.pressedKeys[KeyCode.KEY_K])return false;
        return true;
    }
}


